package comp302.prototype;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

import comp302.prototype.settings.ApplicationSettings;
import comp302.prototype.slide.SlideController;
import comp302.prototype.slide.SlideItem;
import comp302.prototype.xml.XMLConstructor;
import comp302.prototype.xml.XMLNode;
import comp302.prototype.xml.XMLParser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainWindow {
	private static SlideController slideController;

	private JFrame frmPrototype;

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {

			public void run() {

				try {
					MainWindow window = new MainWindow();
					window.frmPrototype.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static SlideController getSlideController() {
		return slideController;
	}

	public MainWindow() {
		initialize();
	}

	private void initialize() {
		frmPrototype = new JFrame();
		JMenuBar menuBar = new JMenuBar();

		JMenu mnFile = new JMenu("File");
		JMenuItem mntmNew = new JMenuItem("New");
		JMenuItem mntmOpenFile = new JMenuItem("Open File...");

		mntmOpenFile.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(frmPrototype);
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					XMLParser parser = new XMLParser(fc.getSelectedFile().getAbsolutePath());
					XMLNode node = parser.parseFile();
					slideController.fromXML(node);
				}
			}
		});

		JMenuItem mntmSave = new JMenuItem("Save");

		mntmSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showSaveDialog(frmPrototype);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					new XMLConstructor(fc.getSelectedFile().getAbsolutePath())
							.write(slideController);
				}
			}
		});

		JMenuItem mntmSaveAs = new JMenuItem("Save As\u2026");
		JMenuItem mntmQuit = new JMenuItem("Quit");

		JMenu mnEdit = new JMenu("Edit");
		JMenuItem mntmCut = new JMenuItem("Cut");
		JMenuItem mntmCopy = new JMenuItem("Copy");
		JMenuItem mntmPaste = new JMenuItem("Paste");

		PreviewPanel previewPanel;
		ToolsPanel toolsPanel;
		SlidePanel slidePanel;
		OptionsPanel optionsPanel;

		frmPrototype.setResizable(false);
		frmPrototype.setTitle("Prototype 1.0");
		frmPrototype.setBounds(100, 100, 740, 530);
		frmPrototype.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmPrototype.setJMenuBar(menuBar);
		frmPrototype.getContentPane().setLayout(null);

		mntmQuit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		previewPanel = new PreviewPanel();
		previewPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		previewPanel.setBounds(6, 56, 113, 424);
		frmPrototype.getContentPane().add(previewPanel);

		toolsPanel = new ToolsPanel();
		toolsPanel.setBorder(null);
		toolsPanel.setBounds(646, 56, 88, 424);
		frmPrototype.getContentPane().add(toolsPanel);

		slidePanel = new SlidePanel();
		slidePanel
				.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		slidePanel.setBounds(131, 56, 503, 424);
		frmPrototype.getContentPane().add(slidePanel);

		slideController = new SlideController(previewPanel, slidePanel);
		slideController.show(slideController.appendNew());

		optionsPanel = new OptionsPanel();
		optionsPanel.setBorder(null);
		optionsPanel.setBounds(6, 6, 728, 38);
		frmPrototype.getContentPane().add(optionsPanel);
		ApplicationSettings.setOptionsPanel(optionsPanel);

		menuBar.add(mnFile);
		mnFile.add(mntmNew);
		mnFile.add(mntmOpenFile);
		mnFile.add(mntmSave);
		mnFile.add(mntmSaveAs);
		mnFile.add(mntmQuit);
		menuBar.add(mnEdit);
		mnEdit.add(mntmCut);
		mnEdit.add(mntmCopy);
		mnEdit.add(mntmPaste);
	}
}
